from django.contrib import admin

from accounts.models import *
from accounts.forms import *

class AccountUserAdmin(admin.ModelAdmin):
    list_display = ['username', 'email', 'account', 'number', 'created', 'active']
    list_filter = ['active', 'created', 'account']
    search_fields = ['account__site__name', 'user__email']


class AccountUserInline(admin.TabularInline):
    model = AccountUser
    fields = ('username',)
    
    def get_formset(self, request, obj=None, **kwargs):
        "This prevents a user from adding AccountUsers to an Account until the Account is saved."
        if obj is None:
            self.extra = 0
        else:   
            self.extra = 0 # Since you can't create new users from the admin yet anyway.
        formset = super(AccountUserInline, self).get_formset(request, obj, **kwargs)
        def add_fields(self, form, index):
            super(formset, self).add_fields(form, index)
            if form.instance.id:
                def username_link():
                    return mark_safe("<a href='/auth/user/%(id)s/'>%(username)s [%(label)s]</a>" % {'label':form.instance.account.label,'username':form.instance.username,'id':form.instance.id})
                form.instance.__unicode__ = username_link
        formset.add_fields = add_fields
        return formset


class AccountAdmin(admin.ModelAdmin):
    # TODO: On Edit, should not be able to delete the AccountUse
    # that corresponds to the Account.auth_owner.
    save_on_top = True
    list_display = ['name', 'domain', 'plan', 'next_renewal', 'revenue', 'owner', 'created', 'status', 'billing_info', 'price_override']

    def domain(self, obj):
        return "<a href='http://%(domain)s'>%(domain)s</a>" % {'domain':obj.domain}
    domain.allow_tags = True
    
    def owner(self, obj):
        return "<a href='/auth/user/%s/'>%s</a>" % (obj.auth_owner.id, obj.auth_owner.get_full_name())
    owner.allow_tags = True
    
    search_fields = ['site__name', 'label', 'site__domain', 'number']
    list_filter = ['status', 'created', 'next_renewal']
    inlines = [AccountUserInline]
    
    def subscription_history(self, obj):
        return '<a href="../subscription/?account__id=%s">Subscription history &raquo;</a>' % obj.pk
    subscription_history.allow_tags = True
    
    def billing_info(self, obj):
        return obj.billing_info is not None
    billing_info.boolean = True
    
    def get_form(self, request, obj=None, **kwargs):
        if obj is None:
            self.fieldsets = NewAccountForm.fieldsets
            return NewAccountForm
        else:
            self.fieldsets = EditAccountForm.fieldsets
            return EditAccountForm


class BillingProfileAdmin(admin.ModelAdmin):
    list_display = ['account', 'card_holder', 'card_type', 'card_number', 'expiry_date', 'created',]
    search_fields = ['card_holder', 'account__site__name', 'account__label',]
    list_filter = ['created', 'modified',]
    ordering = ('-created',)
    

class FeatureAdmin(admin.ModelAdmin):
    model = Feature


class RatePlanFeatureAdminInline(admin.TabularInline):
    model = RatePlanFeature


class RatePlanAdmin(admin.ModelAdmin):
    list_display = ['name', 'setup_fee', 'monthly_fee', 'active']
    search_fields = ['name']
    list_filter = ('active', 'created', 'modified')
    exclude = ('created', 'modified')
    inlines = [RatePlanFeatureAdminInline]


class SubscriptionAdmin(admin.ModelAdmin):
    list_display = ['account', 'plan', 'created']
    list_filter = ['plan', 'created', 'modified']
    search_fields = ['account__site__name', 'account__label']

class PaymentAdmin(admin.ModelAdmin):
    search_fields = ('trans_id', 'card_holder', 'account__site__domain', 'account__number')
    list_display = ('trans_id', 'created', 'account', 'amount', 'success')
    list_filter = ('success', 'created',)

admin.site.register(AccountUser, AccountUserAdmin)  
admin.site.register(Account, AccountAdmin)
admin.site.register(BillingProfile, BillingProfileAdmin)
admin.site.register(RatePlan, RatePlanAdmin)
admin.site.register(Feature, FeatureAdmin)
admin.site.register(Subscription, SubscriptionAdmin)
admin.site.register(Payment, PaymentAdmin)
